package cn.wsn2018.tools.ctrl;

import cn.wsn2018.tools.common.CommonValue;
import cn.wsn2018.tools.model.Design.ProtocolDesign;
import cn.wsn2018.tools.model.LoginUser;
import cn.wsn2018.tools.model.MySessionContext;
import cn.wsn2018.tools.service.ProtocolDesignService;
import cn.wsn2018.tools.util.ServletUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/protocol")
/**
 * 用于处理用户协议字段设计的控制类
 */
public class ProtocolDesignController {
    private Logger logger=Logger.getLogger(ProtocolDesignController.class);

    @Autowired
    private ProtocolDesignService protocolDesignService;

    @CrossOrigin
    @RequestMapping(value = "/protocolDesigns/id",method = RequestMethod.GET)
    /**
     * 查询用户设计的协议字段信息
     */
    public void findProtocolDesign(@RequestParam("id")String id, HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        String respJson= protocolDesignService.dealProtocolDesignQuery(id);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }
    @CrossOrigin
    @RequestMapping(value = "/protocolDesigns/username",method = RequestMethod.GET)
    /**
     * 查询某个用户所有设计
     */
    public void listProtocolDesigns(@RequestParam("username")String username,
                                    @RequestParam("pageNum") String pageNum,
                                    @RequestParam("pageSize")String pageSize, HttpServletRequest request, HttpServletResponse response)throws Exception{
        //ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(username, CommonValue.DESIGNMODULE));
        ServletUtil.getInstance().requestOperating(request);
        String respJson= protocolDesignService.dealProtocolDesignsQuery(username,pageNum,pageSize);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }
    @CrossOrigin
    @RequestMapping(value = "/protocolDesigns",method = RequestMethod.POST)
    /**
     * 保存用户设计的协议字段信息
     */
    public void saveProtocolDesign(@RequestParam("token") String token, @RequestBody ProtocolDesign protocolDesign, HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        //ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(myUsername,CommonValue.DESIGNMODULE));
        String respJson= protocolDesignService.dealProtocolDesignSave(protocolDesign,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }
    @CrossOrigin
    @RequestMapping(value = "/protocolDesigns/id",method = RequestMethod.PUT)
    /**
     * 修改设计的协议字段
     */
    public void updateProtocolDesign(@RequestParam("token") String token,
                                     @RequestBody ProtocolDesign protocolDesign, HttpServletRequest request,HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        //ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(myUsername,CommonValue.DESIGNMODULE));
        String respJson = protocolDesignService.dealProtocolDesignUpdate(protocolDesign,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }
    @CrossOrigin
    @RequestMapping(value = "/protocolDesigns/id",method = RequestMethod.DELETE)
    /**
     * 删除用户的某一协议设计
     */
    public void deleteProtocolDesign(@RequestParam("id")String id,
                                     @RequestParam("token") String token, HttpServletRequest request,HttpServletResponse response) throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
       // ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(myUsername,CommonValue.DESIGNMODULE));
        String respJson = protocolDesignService.dealProtocolDesignDelete(id,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);

    }
    @CrossOrigin
    @RequestMapping(value = "/protocolDesigns",method = RequestMethod.DELETE)
    /**
     * 删除用户的多条协议设计
     */
    public void deleteProtocolDesigns(@RequestParam("idList")List<String> idList,
                                      @RequestParam("token") String token,HttpServletRequest request, HttpServletResponse response) throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        //ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(myUsername,CommonValue.DESIGNMODULE));
        String respJson = protocolDesignService.dealProtocolDesignsDelete(idList,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);

    }
    @CrossOrigin
    @RequestMapping(value = "/protocolDesigns/id/functions",method = RequestMethod.GET)
    /**
     * 查询某个协议设计有哪些功能
     */
    public void listProtocolDesignFunctions(@RequestParam("id") String id,HttpServletRequest request,HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        String respJson = protocolDesignService.dealProtocolDesignFunctions(id);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }

}
